//
//  ProgramViewController.m
//  CoolFM
//
//  Created by 曹曹 on 16/8/30.
//  Copyright © 2016年 曹曹. All rights reserved.
//

#import "ProgramViewController.h"
#import "ProgramModel.h"
#import "Record_Plays.h"
#import "Banners.h"
#import "ProgramBannerTableViewCell.h"
#import "ProgramTableViewCell.h"
#import "GXNetTool.h"
#import "CFMacro.h"

#define kTableViewCellReuse @"programTableViewCellReuse"
#define kBannerCellReuse @"programBanner"

@interface ProgramViewController () <UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) ProgramModel *root;

@end

@implementation ProgramViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self handleData];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerNib:[UINib nibWithNibName:@"ProgramTableViewCell" bundle:nil] forCellReuseIdentifier:kTableViewCellReuse];
    [_tableView registerNib:[UINib nibWithNibName:@"ProgramBannerTableViewCell" bundle:nil] forCellReuseIdentifier:kBannerCellReuse];
}

- (void)handleData {
    // 节目页内容
    [GXNetTool GET:kProgramURL(0, 0, 1, 0) andBody:nil andHeader:nil andResponse:GXJSON andSuccessBlock:^(id result) {
        ProgramModel *program = [ProgramModel modelWithDictionary:result];
        _root = program;
        [_tableView reloadData];
    } andFailureBlock:^(NSError *error) {
        
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _root.record_plays.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (_root.record_plays[indexPath.row].banners) {
        ProgramBannerTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kBannerCellReuse];
        cell.banners = _root.record_plays[indexPath.row].banners;
        return cell;
    } else {
        ProgramTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableViewCellReuse];
        cell.record_plays = _root.record_plays[indexPath.row];
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (_root.record_plays[indexPath.row].banners) {
        return 120;
    } else if (indexPath.row == 0) {
        return 120;
    } else {
        return 80;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
